package com.joysuch.wwyt.bp.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.joysuch.wwyt.bp.entity.BpInvFlowFieldMapping;
import com.joysuch.wwyt.bp.entity.vo.InvFlowFieldMappingVo;
import com.joysuch.wwyt.bp.mapper.BpInvFlowFieldMappingMapper;
import com.joysuch.wwyt.bp.service.BpInvFlowFieldMappingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joysuch.wwyt.core.support.Context;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author heqifeng
 * @since 2022-04-14
 */
@Service
public class BpInvFlowFieldMappingServiceImpl extends ServiceImpl<BpInvFlowFieldMappingMapper, BpInvFlowFieldMapping> implements BpInvFlowFieldMappingService {

    @Autowired
    private BpInvFlowFieldMappingMapper mapper;

    @Override
    public Boolean addOrUpdate(InvFlowFieldMappingVo mappingVo) {
        List<Long> deleteIds = mappingVo.getDeleteIds();
        logicDeleteByIds(deleteIds);
        List<BpInvFlowFieldMapping> mappings = mappingVo.getMappings();
        if (CollectionUtil.isNotEmpty(mappings)) {
            return saveOrUpdateBatch(mappings);
        }
        return false;
    }

    private int logicDeleteByIds(List<Long> ids) {
        if (CollectionUtil.isNotEmpty(ids)) {
            return mapper.logicDeleteByIds(ids);
        }
        return 0;
    }

    public List<BpInvFlowFieldMapping> listByType(Integer sourceType) {
        String orgCode = Context.getCompanyCode();
        Long tenantId = Context.getCurrentTenantId();
        return mapper.selectByType(sourceType,orgCode,tenantId);
    }

}
